<?php
include(APP_PATH.'/Lib/Action/GlobalAction.class.php');

class ReviewAction extends GlobalAction {
    public function index(){
        
        $this->setLang();
        
		$this->display();
    }

    //接收登录
    public function ans() {
        $this->varify();

        $uid = intval($_POST['uid']);
        $tAbstract = M('abstract');
        $ret = $tAbstract->field('aid')->where("uid={$uid}")->find();

        if (!$ret) {
            $ans = 0;           //$ans: 0-未提交论文、1-接收、2-拒绝、3-待定
        } else {
            $aid = $ret['aid'];

            $tReview = M('abst_review');
            $ret = $tReview->field('ans')->where("aid={$aid}")->order('mtime DESC')->find();
            if ($ret) {
                $ans = $ret['ans']? 1 : 2;
            } else {
                $ans = 3;
            }

            $tblAdvice = M('abst_review_advice');
            $ret = $tblAdvice->where("aid={$aid}")->find();
            if (!$ret) {
                $this->assign('hashAdvice', 0);
                $this->assign('review_advice', 'Null');
            } else {
                $this->assign('hashAdvice', 1);
                $this->assign('review_advice', $ret['advice']);
            }
        }

        if ($ans === 1) {
            $this->assign('account', array(
                'uid' => $uid,
                'passwd' => $_POST['password']
            ));

            $tSurvey = M('survey');
            $ret = $tSurvey->where("uid={$uid} AND sno=1")->order('mtime DESC')->find();
            if ($ret) {
                $this->assign('haveVoted', true);
            } else {
                $this->assign('haveVoted', false);
            }
            
        }

        $this->assign('review_ans', $ans);
        $this->display();
    }
    
    //接收调查（AJAX）
    public function survey() {
        if($this->isPost()) {
            if (!$this->varify()) {
                echo json_encode(array(
                    'msg' => 'X   Fail  出错了',
                    'stat' => 'fail',
                    'no' => 1
                ));
                return ;
            }

            $data = array(
                'uid' => intval($_POST['uid']),
                'sno' => 1,
                'ans' => intval($_POST['can']),
                'mtime' => time()
            );

            $tSurvey = M('survey');
            $ret = $tSurvey->data($data)->add();

            if ($ret) {
                echo json_encode(array(
                    'msg' => '√   Voted  已投票',
                    'stat' => 'ok',
                    'no' => 99
                ));
            } else {
                echo json_encode(array(
                    'msg' => 'X   Fail   出错了',
                    'stat' => 'fail',
                    'no' => 2
                ));
            }
        }
    }

    //验证账号密码
    private function varify() {
        $uid = intval($_POST['uid']);
        if($uid <= 0) {
            $this->assign('stat', false);
            $this->assign('error', L('wrong-uid'));
            $this->display();
            return false;
        }
        
        $passwd = strtoupper($_POST['password']);
        
        $tblAccount = M('account');
        $ret = $tblAccount->where("uid={$uid}")->find();
        
        if (empty($ret)) {
            $this->assign('stat', false);
            $this->assign('error', L('wrong-uid'));
            return false;
        } elseif ($ret['password'] != $passwd) {
            $this->assign('stat', false);
            $this->assign('error', L('wrong-passwd'));
            return false;
        } else {
            $this->assign('stat', true);
            return true;
        }
    }
}